package cn.com.baiwei.service.impl;

import cn.com.baiwei.mapper.FoodStuffMapper;
import cn.com.baiwei.mapper.OrderFormMapper;
import cn.com.baiwei.mapper.OrderFormParticularsMapper;
import cn.com.baiwei.pojo.FoodStuff;
import cn.com.baiwei.pojo.OrderForm;
import cn.com.baiwei.pojo.OrderFormParticulars;
import io.swagger.models.auth.In;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (OrderFormParticulars)表服务实现类
 *
 * @author vv
 * @since 2021-09-17 10:40:16
 * @version 1.0
 */
@Service("orderFormParticularsService")
public class OrderFormParticularsServiceImpl {
    @Resource
    private OrderFormParticularsMapper orderFormParticularsMapper;

    @Resource
    private FoodStuffMapper foodStuffMapper;

    @Resource
    private OrderFormMapper orderFormMapper;

    /**
     * 根据模糊条件查询总个数
     *
     * @param name 查询条件
     * @return 返回查询到的总个数
     */
    public Map<String, Object> selectForCount(String name) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.orderFormParticularsMapper.selectForCount(name));
        return map;
    }

    /**
     * 查询所有数据
     * @return  返回所有数据
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.orderFormParticularsMapper.selectAll());
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param orderFormId 主键
     * @return 实例对象
     */
    public Map<String, Object> selectByOrderFormId(String orderFormId) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        int orderFormId1 = Integer.parseInt(orderFormId);
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.orderFormParticularsMapper.selectByOrderFormId(orderFormId1));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param index 查询起始位置
     * @param name  查询条件
     * @return 对象列表
     */
    public Map<String, Object> selectForPage(int index, String name) {
    // 获取当前表中的总记录
        int tableCount = this.orderFormParticularsMapper.selectForCount(name);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / 10 + 1;
        // 计算每页开始的下标值
        index = (index - 1) * 10;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.orderFormParticularsMapper.selectForPage(index, name));
        return map;
    }

    /**
     * 新增数据
     *
     * @param orderForm 加菜订单对象
     * @return 实例对象
     */

    public Map<String, Object> insert(OrderForm orderForm) {
        List<OrderFormParticulars> addList = new ArrayList<>();
        List<OrderFormParticulars> list = orderForm.getOrderFormParticulars();
        for (int i = 0; i <list.size() ; i++) {
            OrderFormParticulars orderFormParticulars = orderFormParticularsMapper.selectByOrderFormIdAndFoodStuffId(orderForm.getId(), list.get(i).getFoodStuffId());
            OrderFormParticulars orderFormParticulars1 = list.get(i);
            if(orderFormParticulars!=null){
                orderFormParticulars.setStuffAmount(orderFormParticulars.getStuffAmount() + orderFormParticulars1.getStuffAmount());
                orderFormParticularsMapper.updateById(orderFormParticulars);
                continue;
            }
            OrderFormParticulars orderFormParticulars2 = list.get(i);
            orderFormParticulars2.setOrderFormId(orderForm.getId());
            addList.add(orderFormParticulars2);
        }
        if(addList!=null){
            orderFormParticularsMapper.insert(addList);
        }
        //更新订单的总金额
        Integer id = orderForm.getId();
        OrderForm orderForm1 = orderFormMapper.selectOrderFormById(id);
        String orderMoneyAfter = new BigInteger(orderForm1.getOrderMoney()).add(new BigInteger(orderForm.getOrderMoney())).toString();
        orderForm1.setOrderMoney(orderMoneyAfter);
        orderFormMapper.updateOrderFormById(orderForm1);
        //调用菜品库存减少方法
        foodStuffMapper.updateCutByIds(list);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "加菜成功");
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param orderFormParticulars 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(OrderFormParticulars orderFormParticulars) {
        this.orderFormParticularsMapper.updateById(orderFormParticulars);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(String id) {
        this.orderFormParticularsMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "删除成功");
        return map;
    }
}